Rsqrtgrad

计算 Rsqrt(平方根倒数)操作的梯度。该算子是 Rsqrt 算子的反向传播(backward pass)部分。

\[\text{output}_i = -\frac{1}{2} \times \text{input2}_i \times \text{input1}_i^3\]

其中 input1 是前向传播时 Rsqrt 的输出(即 \(y = \frac{1}{\sqrt{x}}\)),input2 是来自后一层的上游梯度 \(dy\)output 是对原始输入 \(x\) 的梯度 \(dx\)

输入:
  • input1 - 前向传播时 Rsqrt 的输出数据地址(即 \(y = \frac{1}{\sqrt{x}}\))。

  • input2 - 来自后一层的上游梯度数据地址(即 \(dy\))。

  • size - 计算长度。

  • core_mask - 核掩码(仅共享存储版本需要)。

输出:
  • output - 计算出的对原始输入的梯度数据地址(即 \(dx\))。

支持平台:

MT7004

备注

  • MT7004 支持fp16, fp32

共享存储版本:

void fp_rsqrtgrad_s(float *input1, float *input2, float *output, int size, int core_mask)
void hp_rsqrtgrad_s(half *input1, half *input2, half *output, int size, int core_mask)

C调用示例:

 1//MT7004示例
 2#include <stdio.h>
 3#include <rsqrtgrad.h>
 4
 5int main(int argc, char* argv[]) {
 6    // 假设在DDR空间
 7    float *input1 = (float *)0xA0000000;   // Rsqrt的输出 y = 1/sqrt(x)
 8    float *input2 = (float *)0xA1000000;   // 上游梯度 dy
 9    float *output = (float *)0xB0000000;  // 输出梯度 dx
10
11    int size = 1000;
12    int core_mask = 0xff;
13
14    fp_rsqrtgrad_s(input1, input2, output, size, core_mask);
15    return 0;
16}

私有存储版本:

void fp_rsqrtgrad_p(float *input1, float *input2, float *output, int size)
void hp_rsqrtgrad_p(half *input1, half *input2, half *output, int size)

C调用示例:

 1//MT7004示例
 2#include <stdio.h>
 3#include <rsqrtgrad.h>
 4
 5int main(int argc, char* argv[]) {
 6    // 假设在L2空间
 7    float *input1 = (float *)0x10000000;   // Rsqrt的输出 y = 1/sqrt(x)
 8    float *input2 = (float *)0x10001000;   // 上游梯度 dy
 9    float *output = (float *)0x10002000;   // 输出梯度 dx
10
11    int size = 1000;
12
13    fp_rsqrtgrad_p(input1, input2, output, size);
14    return 0;
15}